1 import java.util.*;
2 import java.io.*;
3
4 public class Logging {
5
6 ObjectMonitor myLock;
7 long start;
8 long pause;
9 FileWriter out;
10 boolean isOpen;
11 static String currentLogName;
12
13 Logging() {
14 myLock = new ObjectMonitor();
15 start = 0;
16 isOpen = false;
17
18 }
19
20 public void done() {
21 if (isOpen == false) {
22 System.out.println("LOGGING: Attepted to done() a log that was already closed.");
23 return;
24 }
25 //System.out.println("LOGGING: DONE");
26 try {
27 out.close();
28 } catch (Exception e) {
29 System.out.println("LOGGING: Failed to close log file in done().");
30 }
31 isOpen = false;
32 }
33
34 public void post(String entry) {
35
36 if (isOpen == false) return;
37 //System.out.println("LOGGING: POST");
38
39 long myTime;
40 String postString;
41
42 myLock.lock(true);
43
44 Date time = new Date();
45 myTime = getTiming();
46 try {
47 postString = myTime + " : " + entry + "\n";
48 out.write(postString, 0, postString.length());
49
50 } catch (Exception e) {
51 }
52
53 myLock.lock(false);
54 }
55
56 public String getLog() {
57
58 int chars = 1;
59 char buf[] = new char[2048];
60 FileReader lf = null;
61
62 StringBuffer lb = new StringBuffer();
63
64 if (isOpen == true) {
65 System.out.println("LOGGING: Tried to getlog on an open log.");
66 done();
67 }
68
69 try {
70
71 // Copy the file to the report buffer
72 System.out.println("LOGGING: trying to open " + currentLogName);
73 lf = new FileReader(currentLogName);
74 System.out.println("LOGGING: Opened log file.");
75 while (chars > 0) {
76 chars = lf.read(buf,0,2048);
77 lb.append(buf,0,chars);
78 }
79
80 } catch (Exception e) {
81 System.out.println("LOGGING: Exception failure while trying to getLog(). It should be OK.");
82 } finally {
83 try {
84 lf.close();
85 } catch (Exception e) { }
86 }
87
88 return lb.toString();
89 }
90
91
92 public void clear() {
93
94 if (isOpen == true) {
95 System.out.println("LOGGING: Attempted to clear() log before closing old log. Closing old log.");
96 done();
97 }
98
99 myLock.lock(true);
100 isOpen = false;
101 Date logTime = new Date();
102 try {
103 currentLogName = new String("log-" + logTime.getTime() + ".s");
104 out = new FileWriter(currentLogName);
105 //System.out.println("LOGGING: OPEN " + "log-" + logTime.getTime() + ".s");
106 isOpen = true;
107 myLock.lock(false);
108
109 } catch (Exception e) { myLock.lock(false);
110 System.out.println("LOGGING: Failed to open log file.");
111 //throw(e);
112 } // toss this one at the wind. it is a bad, bad thing
113 }
114
115 public void startTiming() {
116
117 Date time = new Date();
118 start = time.getTime();
119 }
120
121 public void stopTiming() {
122 start = 0;
123 }
124
125 public void pauseTiming() {
126 Date time = new Date();
127 pause = time.getTime();
128 }
129
130 public void resumeTiming() {
131 Date time = new Date();
132 start = time.getTime() - (pause-start);
133 }
134
135
136 public long getTiming() {
137 Date time = new Date();
138 long current;
139 if (start > 0)
140 current = time.getTime() - start;
141 else
142 current = 0;
143 return current;
144 }
145 }
|